package com.ruoyi.mobile.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.mobile.service.*;
import com.ruoyi.mobile.utils.NftResponse;
import com.ruoyi.mobile.utils.Paginator;
import com.ruoyi.mobile.utils.ResponseConstants;
import com.ruoyi.mobile.utils.WxCode;
import com.ruoyi.system.api.req.*;
import com.ruoyi.system.api.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/userapp")
@Api(tags = "用户手机端模块")
public class UserAppController {
    @Autowired
    private AuthService authService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IShProductService productService;
    @Autowired
    private IShCourseTeacherService teacherService;
    @Autowired
    private IShCourseService courseService;
    @Autowired
    private IShAppointmentService shAppointmentService;
    @Autowired
    private IShOrderService orderService;
    @Autowired
    private IShMarketService marketService;
    @Autowired
    private ISysUserAddressService sysUserAddressService;

    @ApiOperation(value="商品大类")
    @RequestMapping(value = "/getProductTypes", method = RequestMethod.GET)
    public NftResponse<List<DictResp>> getProductTypes() {
        return productService.getProductTypes();
    }
    @ApiOperation(value="点餐大类")
    @RequestMapping(value = "/getMealTypes", method = RequestMethod.GET)
    public NftResponse<List<DictResp>> getMealTypes() {
        return productService.getMealTypes();
    }
    @ApiOperation(value="培训大类")
    @RequestMapping(value = "/getTrainTypes", method = RequestMethod.GET)
    public NftResponse<List<DictResp>> getTrainTypes() {
        return productService.getTrainTypes();
    }
    @ApiOperation(value="商铺分页信息")
    @RequestMapping(value = "/getShopList", method = RequestMethod.POST)
    public NftResponse<Paginator<SysDeptResp>> getSchoolList(@RequestBody SysDeptReq query) {
        return sysDeptService.getShopList(query);
    }
    @ApiOperation(value="商铺详细信息")
    @RequestMapping(value = "/getShopInfo", method = RequestMethod.GET)
    public NftResponse<SysDeptResp> getShopInfo(@RequestParam("deptId") Long deptId) {
        return sysDeptService.getShopInfoById(deptId);
    }
    @ApiOperation(value="商铺全部列表信息")
    @RequestMapping(value = "/getShopAllList", method = RequestMethod.POST)
    public NftResponse<Paginator<SysDeptResp>> getShopAllList(@RequestBody SysDeptReq query) {
        return sysDeptService.getShopAllList(query);
    }
    @ApiOperation(value="商铺全部列表信息")
    @RequestMapping(value = "/getTrainAllList", method = RequestMethod.POST)
    public NftResponse<Paginator<SysDeptResp>> getTrainAllList(@RequestBody SysDeptReq query) {
        return sysDeptService.getTrainAllList(query);
    }
    @ApiOperation(value="商品信息")
    @RequestMapping(value = "/getProductList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShProductResp>> getProductList(@RequestBody ShProductReq query) {
        return productService.getProductList(query);
    }
    @ApiOperation(value="餐品信息")
    @RequestMapping(value = "/getMealList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShProductResp>> getMealList(@RequestBody ShProductReq query) {
        return productService.getMealList(query);
    }
    @ApiOperation(value="培训信息")
    @RequestMapping(value = "/getTrainList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShProductResp>> getTrainList(@RequestBody ShProductReq query) {
        return productService.getTrainList(query);
    }
    @ApiOperation(value="商品详细")
    @RequestMapping(value = "/getProductInfo", method = RequestMethod.GET)
    public NftResponse<ShProductResp> getProductInfo(@RequestParam("id") Long id, @RequestParam("userId") Long userId) {
        return productService.getProductInfo(id, userId);
    }
    @ApiOperation(value="课程教师信息")
    @RequestMapping(value = "/getTeacherList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShCourseTeacherResp>> getTeacherList(@RequestBody ShCourseTeacherReq query) {
        return teacherService.getTeacherList(query);
    }
    @ApiOperation(value="课程教师信息")
    @RequestMapping(value = "/getTeacherjkList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShCourseTeacherResp>> getTeacherjkList(@RequestBody ShCourseTeacherReq query) {
        return teacherService.getTeacherjkList(query);
    }
    @ApiOperation(value="课程教师详细信息")
    @RequestMapping(value = "/getTeacherInfo", method = RequestMethod.GET)
    public NftResponse<ShCourseTeacherResp> getTeacherInfo(@RequestParam("id") Long id) {
        return teacherService.getTeacherInfo(id);
    }
    @ApiOperation(value="课程教师详细信息")
    @RequestMapping(value = "/getTeacherjkInfo", method = RequestMethod.GET)
    public NftResponse<ShCourseTeacherResp> getTeacherjkInfo(@RequestParam("id") Long id) {
        return teacherService.getTeacherjkInfo(id);
    }
    @ApiOperation(value="课程信息")
    @RequestMapping(value = "/getCourseList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShCourseResp>> getCourseList(@RequestBody ShCourseReq query) {
        return courseService.getCourseList(query);
    }
    @ApiOperation(value="课程详细")
    @RequestMapping(value = "/getCourseInfo", method = RequestMethod.GET)
    public NftResponse<ShCourseResp> getCourseInfo(@RequestParam("id") Long id) {
        return courseService.getCourseInfo(id);
    }
    @ApiOperation(value="创建订单")
    @RequestMapping(value = "/createOrder", method = RequestMethod.PUT)
    public NftResponse<JSONObject> createOrder(@RequestBody List<ShOrderReq> queryList) {
        try {
            return orderService.insertOrderInfos(queryList);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "下单失败", null);
        }
    }
    @ApiOperation(value="修改订单状态")
    @RequestMapping(value = "/updateOrder", method = RequestMethod.PUT)
    public NftResponse<Boolean> updateOrder(@RequestParam("orderId") Long orderId) {
        try {
            return orderService.updateOrderInfo(orderId);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "修改失败", false);
        }
    }
    @ApiOperation(value="订单支付")
    @RequestMapping(value = "/pay/{fee}/{tradeNo}/{userId}/{appId}", method = RequestMethod.GET)
    public NftResponse<JSONObject> wxPayFunction(@PathVariable String fee,
                                                 @PathVariable String tradeNo,
                                                 @PathVariable Long userId,
                                                 @PathVariable String appId) {
        try {
            return orderService.wxPayFunction(appId, fee, tradeNo, userId);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "支付失败", null);
        }
    }
    @ApiOperation(value="取消订单")
    @RequestMapping(value = "/delOrder", method = RequestMethod.PUT)
    public NftResponse<Boolean> delOrder(@RequestBody ShOrderReq query) {
        try {
            return orderService.deleteOrderInfo(query);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "删除失败", false);
        }
    }
    @ApiOperation(value="创建预约")
    @RequestMapping(value = "/createDating", method = RequestMethod.PUT)
    public NftResponse<Integer> createDating(@RequestBody ShAppointmentReq query) {
        try {
            return shAppointmentService.insertDatingInfo(query);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "预约失败", 0);
        }
    }
    @ApiOperation(value="取消预约")
    @RequestMapping(value = "/delDating", method = RequestMethod.PUT)
    public NftResponse<Boolean> delDating(@RequestBody ShAppointmentReq query) {
        try {
            return shAppointmentService.deleteDatingInfo(query);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "删除失败", false);
        }
    }
    @ApiOperation(value="预约信息")
    @RequestMapping(value = "/getDatingList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShAppointmentResp>> getDatingList(@RequestBody ShAppointmentReq query) {
        return shAppointmentService.getDatingList(query);
    }
    @ApiOperation(value="订单信息")
    @RequestMapping(value = "/getOrderList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShOrderResp>> getOrderList(@RequestBody ShOrderReq query) {
        return orderService.getOrderList(query);
    }
    @ApiOperation(value="订单支付信息")
    @RequestMapping(value = "/getOrderPayList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShOrderResp>> getOrderPayList(@RequestBody ShOrderReq query) {
        return orderService.getOrderPayList(query);
    }
    @ApiOperation(value="分销信息")
    @RequestMapping(value = "/getMarketList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShMarketResp>> getMarketList(@RequestBody ShMarketReq query) {
        return marketService.getMarketList(query);
    }
    @ApiOperation(value="清空分销信息")
    @RequestMapping(value = "/clearMarket", method = RequestMethod.PUT)
    public NftResponse<Boolean> clearMarket(@RequestBody ShMarketReq query) {
        try {
            return marketService.clearMarket(query);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "删除失败", false);
        }
    }

    @ApiOperation(value="修改姓名")
    @RequestMapping(value = "/putUserName", method = RequestMethod.PUT)
    public NftResponse<Boolean> putUsername(@RequestParam("userId") Long userId, @RequestParam("userName") String userName) {
        try {
            String updateUserName = java.net.URLDecoder.decode(userName, "UTF-8");
            return authService.putUsername(userId, updateUserName);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "修改失败", false);
        }
    }

    @ApiOperation(value="修改性别")
    @RequestMapping(value = "/putSex", method = RequestMethod.PUT)
    public NftResponse<Boolean> putSex(@RequestParam("userId") Long userId, @RequestParam("sex") String sex) {
        try {
            return authService.putSex(userId, sex);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "修改失败", false);
        }
    }

    @ApiOperation(value="修改头像")
    @RequestMapping(value = "/putAvatar", method = RequestMethod.POST)
    public NftResponse<String> putAvatar(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) {
        String avatar = authService.putAvatar(request,file);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "设置成功", avatar);
    }

    @ApiOperation(value="获取地址列表")
    @RequestMapping(value = "/getAddressList", method = RequestMethod.GET)
    public NftResponse<List<SysUserAddressResp>> getAddressList(@RequestParam("userId") Long userId) {
        SysUserAddressReq query = new SysUserAddressReq();
        query.setUserId(userId);
        return sysUserAddressService.getAddressList(query);
    }

    @ApiOperation(value="获取地址信息")
    @RequestMapping(value = "/getAddrInfo", method = RequestMethod.GET)
    public NftResponse<SysUserAddressResp> getAddrInfo(@RequestParam("id") Long id) {
        SysUserAddressReq query = new SysUserAddressReq();
        query.setId(id);
        return sysUserAddressService.getAddrInfo(id);
    }

    @ApiOperation(value="添加地址")
    @RequestMapping(value = "/addAddress", method = RequestMethod.PUT)
    public NftResponse<Boolean> addAddress(@RequestBody SysUserAddressReq query) {
        try {
            return sysUserAddressService.insertAddressInfo(query);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "添加失败", false);
        }
    }

    @ApiOperation(value="修改地址")
    @RequestMapping(value = "/editAddress", method = RequestMethod.PUT)
    public NftResponse<Boolean> editAddress(@RequestBody SysUserAddressReq query) {
        try {
            return sysUserAddressService.updateAddressInfo(query);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "修改失败", false);
        }
    }

    @ApiOperation(value="删除地址")
    @RequestMapping(value = "/delAddress", method = RequestMethod.PUT)
    public NftResponse<Boolean> delAddress(@RequestBody SysUserAddressReq query) {
        try {
            return sysUserAddressService.deleteAddressInfo(query);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "删除失败", false);
        }
    }

    @ApiOperation(value="获取微信用户手机号")
    @RequestMapping(value = "/getPhone", method = RequestMethod.GET)
    public NftResponse<String> getPhone(@RequestParam("appid") String appid,
                                        @RequestParam("encryptedData") String encryptedData,
                                        @RequestParam("iv") String iv,
                                        @RequestParam("sessionKey") String sessionKey,
                                        @RequestParam("openid") String openid,
                                        @RequestParam("inviteId") Long inviteId) {
        String phonenumber = WxCode.getInstance().getMinPhonenumber(encryptedData, iv, sessionKey);
        if (null!=phonenumber&&!"".equals(phonenumber)) {
            authService.putOpenId(appid, phonenumber, openid, inviteId);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", phonenumber);
    }

    @ApiOperation(value="微信用户信息")
    @RequestMapping(value = "/wxUser", method = RequestMethod.GET)
    public NftResponse<SysUserResp> wxUser(@RequestParam("openId") String openId) {
        return authService.getUserInfo(openId);
    }

    @PostMapping("/notify")
    public NftResponse<String> wxpayNotify(HttpServletRequest request, HttpServletResponse response) {
        return orderService.wxpayNotify(request, response);
    }

    @ApiOperation(value="广告分页信息")
    @RequestMapping(value = "/getBannerList", method = RequestMethod.POST)
    public NftResponse<Paginator<ShBannerResp>> getBannerList(@RequestBody ShBannerReq query) {
        return sysDeptService.getBannerList(query);
    }

    @ApiOperation(value="广告详细信息")
    @RequestMapping(value = "/getBannerInfo", method = RequestMethod.GET)
    public NftResponse<ShBannerResp> getBannerInfo(@RequestParam("id") Long id) {
        return sysDeptService.getBannerInfo(id);
    }
}
